package com.sky.controller.user;

import com.sky.entity.AddressBook;
import com.sky.result.Result;
import com.sky.service.AddressBookService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 类描述：
 */
@Api(tags = "用户地址管理接口")
@RestController
@RequestMapping("/user/addressBook")
public class AddressBookController {
    @Autowired
    private AddressBookService addressBookService;

    @ApiOperation("添加地址")
    @PostMapping
    public Result saveAddressBook(@RequestBody AddressBook addressBook) {
        addressBookService.saveAddressBook(addressBook);
        return Result.success();
    }

    @ApiOperation("获取所有地址")
    @GetMapping("/list")
    public Result getAllAddressBook() {
        List<AddressBook> addressBooks = addressBookService.getAllAddressBook();
        return Result.success(addressBooks);
    }

    @ApiOperation("获取默认地址")
    @GetMapping("/default")
    public Result getDefaultAddressBook() {
        AddressBook addressBook = addressBookService.getDefaultAddressBook();
        return Result.success(addressBook);
    }

    @ApiOperation("根据ID查询地址")
    @GetMapping("/{id}")
    public Result getAddressBookById(Long id) {
        AddressBook addressBook = addressBookService.getAddressBookById(id);
        return Result.success(addressBook);
    }

    @ApiOperation("修改地址")
    @PutMapping
    public Result updateAddressBook(@RequestBody AddressBook addressBook) {
        addressBookService.updateAddressBook(addressBook);
        return Result.success();
    }

    @ApiOperation("删除地址")
    @DeleteMapping
    public Result deleteAddressBookById(Long id) {
        addressBookService.deleteAddressBookById(id);
        return Result.success();
    }

    @ApiOperation("设置默认地址")
    @PutMapping("/default")
    public Result setDefaultAddressBook(@RequestBody AddressBook addressBook) {
        addressBookService.setDefaultAddressBook(addressBook);
        return Result.success();
    }
}